<template>
    <div>
<van-address-edit
  :area-list="areaList"
  show-postal
  show-set-default
  show-search-result
  :search-result="searchResult"
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @change-detail="onChangeDetail"
  @change-area="getareaCode"
/>
    </div>
</template>

<script>
import { ref } from 'vue';
import { Toast } from 'vant';
import { areaList } from '@vant/area-data';
import {fetchaddaddress} from '@/API/goods.js'
export default {
  setup() {
    const searchResult = ref([]);
    const dizhi =ref('')
    const areaCode=ref('')
    const onDelete = () => Toast('delete');
    const onChangeDetail = (val) => {
      console.log(val);

    };
    return {

      onDelete,
      areaList,
      areaCode,
      onChangeDetail,
      dizhi
    };
  },
  methods:{
     async  onSave(addressInfo) {
        let user_id = this.$store.state.userInfo.id;
        addressInfo.isDefault =  addressInfo.isDefault ? 1 : 0;
        addressInfo.country = addressInfo.county;

        let formData = {
        
            ...addressInfo,
          areaCode:this.areaCode,
        }
        console.log('添加的数据',formData);
        let result = await fetchaddaddress(user_id,formData)
        console.log(result);
        if(result.status ==1){
          Toast('添加失败',result.message)
       
        }else{
            Toast('添加成功',result.message)
             this.$router.back()
        }
       },
       getareaCode(values){
              this.areaCode = values.map(item => item.code).join('-')
                let user_id = this.$store.state;
              console.log( this.areaCode ,user_id);
       }
  }
};
</script>

<style lang="scss" scoped>

</style>